package com.lk.springboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lk.springboot.bean.Press;
import com.lk.springboot.dao.PressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author lk
 * @date 2020/1/5 0005 0:24
 */
@Controller
public class PressController {


    @Autowired
    PressMapper PressMapper;

    @RequestMapping("/addPress")
    public String listPress(Press p) throws Exception {
        PressMapper.save(p);
        return "redirect:listPress";
    }

    @RequestMapping("/deletePress")
    public String deletePress(Press p) throws Exception {
        PressMapper.delete(p.getP_id());
        return "redirect:listPress";
    }

    @RequestMapping("/updatePress")
    public String updatePress(Press p) throws Exception {
        PressMapper.update(p);
        return "redirect:listPress";
    }

    @RequestMapping("/listPress")
    public String listPress(Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
        PageHelper.startPage(start, size, "p_id");
        List<Press> cs = PressMapper.findAll();
        PageInfo<Press> page = new PageInfo<>(cs);
        m.addAttribute("page", page);
        return "listPress";
    }

    @RequestMapping("/searchPress")
    public String searchList(Press p, Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
        PageHelper.startPage(start, size, "p_id");
        List<Press> cs = null;
        if (p.getP_name() != "") {
            cs = PressMapper.findByName(p.getP_name());
        } else if (p.getP_person() != ""){
            cs = PressMapper.findByPerson(p.getP_person());
        }else if (p.getP_name()== "" && p.getP_person() == ""){
            cs = PressMapper.findAll();
        }
        PageInfo<Press> page = new PageInfo<>(cs);
        m.addAttribute("page", page);
        return "listPress";
    }
}
